package com.subway.babynote;
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.ListAdapter;
import android.widget.ListView;

/**

*
*/
public class FocusListView extends ListView
{
    /**
     * 
     * @param context context
     */
    public FocusListView(Context context)
    {
        super(context);
    }
   
  
    public FocusListView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }
   
    /**
   
     * @param context context
     * @param attrs attrs
     * @param defStyle defStyle
     */
    public FocusListView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
    }
   
    /**
     * ListView��ȡ����/ʧȥ����
     * @param gainFocus ListView�Ƿ��ý���
     * @param direction
     * @param previouslyFocusedRect previouslyFocusedRect
     */
    protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect)
    {
        super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
        if (gainFocus && previouslyFocusedRect != null)
        {
            final ListAdapter listAdapter = getAdapter();
            final int adapterCount = listAdapter.getCount();
            switch (direction)
            {
                case FOCUS_DOWN:
                    for (int i = 0; i < adapterCount; i++)
                    {
                    
                        if (listAdapter.isEnabled(i))
                        {
                            setSelection(i);
                            break;
                        }
                    }
                    break;
                case FOCUS_UP:
                    for (int i = adapterCount - 1; i >= 0; i--)
                    {
                     
                        if (listAdapter.isEnabled(i))
                        {
                            setSelection(i);
                            break;
                        }
                    }
                    break;
                default:
                    break;
            }
        }
    }
   
}